/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007-2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.basics.manager.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.betwixt.strategy.TypeBindingStrategy;

import com.genia.toolbox.basics.bean.RegisterableTypeBindingStrategy;

/**
 * {@link TypeBindingStrategy} that can be dynamically defined.
 */
public class AggregatorTypeBindingStrategy
    extends TypeBindingStrategy
{

  /**
   * the registered binding strategies.
   */
  private List<RegisterableTypeBindingStrategy> bindingStrategies = new ArrayList<RegisterableTypeBindingStrategy>();

  /**
   * cached values for {@link BindingType}.
   */
  private final Map<Class<?>, BindingType> bindingStrategiesByType = new HashMap<Class<?>, BindingType>();



  /**
   * Gets the binding type to be used for the given Java type.
   * 
   * @param type
   *                <code>Class</code> for which the binding type is to be
   *                determined, not null
   * @return <code>BindingType</code> enumeration indicating the type of
   *         binding, not null
   * @see org.apache.commons.betwixt.strategy.TypeBindingStrategy#bindingType(java.lang.Class)
   */
  @Override
  public BindingType bindingType(@SuppressWarnings("unchecked")
  Class type)
  {
    if (!bindingStrategiesByType.containsKey(type)) {
      bindingStrategiesByType.put(type, TypeBindingStrategy.DEFAULT.bindingType(type));
      for (RegisterableTypeBindingStrategy registerableTypeBindingStrategy : getBindingStrategies()) {
        if (registerableTypeBindingStrategy.getType().isAssignableFrom(type)) {
          bindingStrategiesByType.put(type, registerableTypeBindingStrategy.getBindingType());
          break;
        }
      }
    }
    return bindingStrategiesByType.get(type);
  }



  /**
   * getter for the bindingStrategies property.
   * 
   * @return the bindingStrategies
   */
  public List<RegisterableTypeBindingStrategy> getBindingStrategies()
  {
    return bindingStrategies;
  }



  /**
   * setter for the bindingStrategies property.
   * 
   * @param bindingStrategies
   *                the bindingStrategies to set
   */
  public void setBindingStrategies(List<RegisterableTypeBindingStrategy> bindingStrategies)
  {
    this.bindingStrategies = bindingStrategies;
  }

}
